package edu.ymu.cms.service.impl;

import edu.ymu.cms.dao.*;
import edu.ymu.cms.model.*;
import edu.ymu.cms.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import edu.ymu.cms.service.ManageService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private NewsDao newsDao;

    @Autowired
    private CarouselDao carouselDao;

    @Autowired
    private SinglepageDao singlepageDao;

    @Autowired
    private UsersDao usersDao;

    @Autowired
    private BookDao bookDao;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private ClassroomDao classroomDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private TeacherDao teacherDao;


    @Override
    public boolean addNews(News news) {

        try {
            newsDao.save(news);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public boolean deleteNews(String id) {
        try {
            newsDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean updateNews(News news) {
        try {
            newsDao.save(news);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public News getNews(String id) {
        return newsDao.findOne(id);
    }

    @Override
    public List<News> getNewsList() {
        return newsDao.findAll();
    }

    @Override
    public boolean addCarousel(Carousel carousel) {
        try {
            carouselDao.save(carousel);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteCarousel(String id) {
        try {
            carouselDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateCarousel(Carousel carousel) {
        try {
            carouselDao.save(carousel);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Carousel getCarousel(String id) {
        return carouselDao.findOne(id);
    }

    @Override
    public List<Carousel> getCarouselList() {
        return carouselDao.findAll();
    }



    @Override
    public boolean addSinglepage(Singlepage singlepage) {
        try {
            singlepageDao.save(singlepage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteSinglepage(String id) {
        try {
            singlepageDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateSinglepage(Singlepage singlepage) {
        try {
            singlepageDao.save(singlepage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Singlepage getSinglepage(String id) {
        return singlepageDao.findOne(id);
    }

    @Override
    public List<Singlepage> getSinglepageList() {
        return singlepageDao.findAll();
    }

    @Override
    public Users getUsersByUsernameAndPass(String username, String pass) {
        return usersDao.findUsersByUsernameAndAndPass(username,pass);
    }

    @Override
    public String getCurrentUserId() {
        String userid= (String) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
        if(Tools.isNullOrSpace(userid)){
            return null;
        }
        else {
            return userid;
        }
    }

    @Override
    public String getCurrentUserRole() {
        String role=null;
        Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>)    SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        for (GrantedAuthority authority : authorities) {
            role = authority.getAuthority();

        }

        if(Tools.isNullOrSpace(role)){
            return null;
        }
        else{
            return role;
        }
    }


    @Override
    public boolean addBook(Book book) {

        try {
            bookDao.save(book);
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public boolean deleteBook(String id) {

        try {
            bookDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public boolean updateBook(Book book) {

        try {
            bookDao.save(book);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Book getBook(String id) {
        return bookDao.findOne(id);
    }

    @Override
    public List<Book> getBookList() {
        return bookDao.findAll();
    }


    @Override
    public boolean addCourse(Course course) {
        try {
            courseDao.save(course);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }


    @Override
    public boolean deleteCourse(String id) {
        return false;
    }

    @Override
    public boolean updateCourse(Course course) {
        return false;
    }


    @Override
    public boolean addStudent(Student student) {

        try {
            studentDao.save(student);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteStudent(String id) {
        try {
            studentDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateStudent(Student student) {
        try {
            studentDao.save(student);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Student getStudent(String id) {
        return studentDao.findOne(id);
    }

    @Override
    public List<Student> getStudentList() {
        return studentDao.findAll();
    }

    @Override
    public boolean addTeacher(Teacher teacher) {
        try {
            teacherDao.save(teacher);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteTeacher(String id) {
        try {
            teacherDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateTeacher(Teacher teacher) {
        try {
            teacherDao.save(teacher);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Teacher getTeacher(String id) {
        return teacherDao.findOne(id);
    }

    @Override
    public List<Teacher> getTeacherList() {
        return teacherDao.findAll();
    }

    //教室表
    @Override
    public boolean addClassroom(Classroom classroom) {
        try {
            classroomDao.save(classroom);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteClassroom(String id) {
        try {
            classroomDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateClassroom(Classroom classroom) {
        try {
            classroomDao.save(classroom);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Classroom getClassroom(String id) {
        return classroomDao.findOne(id);
    }

    @Override
    public List<Classroom> getClassroomList() {
        return classroomDao.findAll();
    }


    @Override
    public Course getCourse(String id) {
        return courseDao.getOne(id);
    }

    @Override
    public List<Course> getCourseList() {
        return courseDao.findAll();
    }

    //查询
    @Override
    public List<Course> getCourseListByBook(String id) {

        return courseDao.getCourseListByBook(id);
    }

    @Override
    public List<Course> getCourseListByStudent(String id) {
        return courseDao.getCourseListByStudent(id);
    }

    @Override
    public List<Course> getCourseListByClassroom(String id) {
        return courseDao.getCourseListByClassroom(id);
    }

    @Override
    public List<Course> getCourseListByTeacher(String id) {
        return courseDao.getCourseListByTeacher(id);
    }


}
